
-- $Id: Delegates.lua 72 2009-04-06 11:38:08Z Fylgjas $

-- ========================================================================= --
-- Copyright © 2009 Thomas ROBERT (aka Fylgjas EU - Marécage de Zangar)      --
-- ========================================================================= --
-- This file is part of FyLooter.                                            --
--                                                                           --
-- FyLooter is free software: you can redistribute it and/or modify it under --
-- the terms of the GNU General Public License as published by the Free      --
-- Software Foundation, either version 3 of the License, or (at your option) --
-- any later version.                                                        --
--                                                                           --
-- FyLooter is distributed in the hope that it will be useful, but WITHOUT   --
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     --
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for  --
-- more details.                                                             --
--                                                                           --
-- You should have received a copy of the GNU General Public License along   --
-- with FyLooter. If not, see <http://www.gnu.org/licenses/>.                --
-- ========================================================================= --

local revision = tonumber(string.sub("$Rev: 72 $", 7, -3));

if (FyLooter.revision < revision) then
  FyLooter.revision = revision;
end

-- ========================================================================= --

-- 
-- _select
--
local function _select(self)
  local idx = self:GetID() + FyLooter.ScrollFrame:GetOffset();
--  local sel = FyLooter.ScrollFrame.model:GetSelection();

--  if (idx == sel) then
--    FyLooter.ScrollFrame.model:SetSelection(nil);
--  else
    FyLooter.ScrollFrame.model:SetSelection(idx);
--  end
end

-- 
-- _expand
--
local function _expand(self)
  local idx = self:GetParent():GetID() + FyLooter.ScrollFrame:GetOffset();
  local isExpanded = select(7, FyLooter.CharactersModel:GetData(idx));

  if (isExpanded) then
    FyLooter.ScrollFrame.model:Collapse(idx);
  else
    FyLooter.ScrollFrame.model:Expand(idx);
  end
end

-- ========================================================================= --

-- FyLooter.Delegates
FyLooter.Delegates = {};

setmetatable(FyLooter.Delegates, { __index = function(self, i)
  -- Delegate
  local Delegate = CreateFrame("Button", nil, FyLooter.ScrollFrame.Viewport);
  Delegate:SetID(i);
  Delegate:SetHeight(16);
  Delegate:SetPoint("LEFT", 4, 0);
  Delegate:SetPoint("RIGHT", -4, 0);
  Delegate:SetPoint("TOP", 0, 16 * (1 - i) - 4);
  Delegate:SetNormalTexture("Interface\\AddOns\\FyLooter\\Textures\\BantoBar");
  Delegate:SetHighlightTexture("Interface\\FriendsFrame\\UI-FriendsFrame-HighlightBar", "ADD");
  Delegate:SetScript("OnClick", _select);
  Delegate:GetNormalTexture():SetVertexColor(0.5, 0.5, 0.5, 0.5);

  -- Delegate.ExpandButton
  Delegate.ExpandButton = CreateFrame("Button", nil, Delegate);
  Delegate.ExpandButton:SetWidth(16);
  Delegate.ExpandButton:SetHeight(16);
  Delegate.ExpandButton:SetPoint("LEFT", 2, 0);
  Delegate.ExpandButton:SetHighlightTexture("Interface\\Buttons\\UI-PlusButton-Hilight", "ADD");
  Delegate.ExpandButton:SetScript("OnClick", _expand);
    
  -- Delegate.NameLabel
  Delegate.NameLabel = Delegate:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall");
  Delegate.NameLabel:SetWidth(78);
  Delegate.NameLabel:SetHeight(16);
  Delegate.NameLabel:SetPoint("LEFT", Delegate.ExpandButton, "RIGHT", 4, 0);
  Delegate.NameLabel:SetJustifyH("LEFT");

  -- Delegate.LevelLabel
  Delegate.LevelLabel = Delegate:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall");
  Delegate.LevelLabel:SetWidth(20);
  Delegate.LevelLabel:SetHeight(16);
  Delegate.LevelLabel:SetPoint("LEFT", Delegate.NameLabel, "RIGHT", 4, 0);
    
  -- Delegate.ClassLabel
  Delegate.ClassLabel = Delegate:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall");
  Delegate.ClassLabel:SetHeight(16);
  Delegate.ClassLabel:SetPoint("LEFT", Delegate.LevelLabel, "RIGHT", 4, 0);
  Delegate.ClassLabel:SetPoint("RIGHT", -4, 0);
  Delegate.ClassLabel:SetJustifyH("LEFT");

  -- 
  -- Delegate:SetData()
  --
  -- TODO player, character, level, class, color, hasChild, isExpanded, status
    
  self[i] = Delegate;
  return Delegate;
end });

-- ========================================================================= --

